﻿using System.Collections.Generic;

namespace WODPL.Models
{
    public class SelectionModel
    {
        private GameMenuItemModel selectedModel;
        private IList<GameMenuItemModel> menuItemModel;

        public SelectionModel(IList<GameMenuItemModel> menuItemModel)
        {
            this.menuItemModel = menuItemModel;
            selectedModel = null;
        }

        public bool IsSelected(GameMenuItemModel selectedModel)
        {
            return this.selectedModel == null ? false : this.selectedModel == selectedModel;
        }

        public void SelectModel(GameMenuItemModel itemModel)
        {
            selectedModel = itemModel;
        }

        public static SelectionModel operator ++(SelectionModel model)
        {
            for (int i = 0; i < model.menuItemModel.Count - 1; i++)
            {
                if (model.menuItemModel[i] == model.selectedModel)
                {
                    model.selectedModel = i < model.menuItemModel.Count - 1
                                              ? model.menuItemModel[i + 1]
                                              : model.menuItemModel[0];
                    return model;
                }
            }
            model.selectedModel = model.menuItemModel[0];
            return model;
        }

        public static SelectionModel operator --(SelectionModel model)
        {
            for (int i = model.menuItemModel.Count - 1; i > 0; i--)
            {
                if (model.menuItemModel[i] == model.selectedModel)
                {
                    model.selectedModel = i > 0
                                              ? model.menuItemModel[i - 1]
                                              : model.menuItemModel[model.menuItemModel.Count - 1];
                    return model;
                }
            }
            model.selectedModel = model.menuItemModel[model.menuItemModel.Count - 1];
            return model;
        }
    }
}